package com.zhu.service.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhu.model.pojo.Result;
import com.zhu.model.pojo.model.system.SysUser;

import com.zhu.model.pojo.model.system.vo.SysUserQueryVo;
import com.zhu.service.system.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-10-29
 */
@RestController
@RequestMapping("/admin/system/sysUser")
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 查询用户列表
     * @param page
     * @param limit
     * @param userQueryVo
     * @return
     */
    @ApiOperation(value = "用户列表")
    @GetMapping("/{page}/{limit}")
    public Result pageList(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo userQueryVo){
        System.out.println(userQueryVo);
        Page<SysUser> userPage = new Page<>(page, limit);
        IPage<SysUser> sysUserIPage= userService.selectPage(userPage,userQueryVo);
       return Result.ok(sysUserIPage);
    }

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
        if(userService.save(sysUser)){
            return Result.ok().message("添加成功！");
        }
      return Result.fail().message("添加失败！");
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "根据id删除用户")
    public Result removeById(@PathVariable Long id){
        if(userService.removeById(id)){
            return Result.ok().message("删除成功！");
        }
        return Result.fail().message("删除失败！");
    }

    /**
     * 批量删除
     * @param idList
     * @return
     */
    @DeleteMapping("/removeBatch")
    @ApiOperation(value = "批量删除")
    public Result removeBatch(@RequestBody List<Long> idList){
        if(userService.removeBatch(idList)){
            return Result.ok().message("删除成功！");
        }
        return Result.fail().message("删除失败！");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询用户")
    public Result findById(@PathVariable Long id){
        SysUser sysUser = userService.getById(id);
        return Result.ok(sysUser);
    }

    @PutMapping("/update")
    @ApiOperation(value = "根据更新用户")
    public Result update(@RequestBody SysUser user){
        if(userService.updateById(user)){
            return Result.ok().message("更新成功！");
        }
        return Result.fail().message("更新失败！");
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        userService.updateStatus(id, status);
        return Result.ok();
    }

    @GetMapping("/test")
    @ApiOperation(value = "测试")
    public Result test(SysUser sysUserQueryVo){
        return Result.ok(sysUserQueryVo);
    }


}
